package com.gxljc.bear.crawler.related;

import java.io.IOException;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * To find out all the movies in the same name
 * 
 * @author yubiao,yihao
 * @since 2015-6-3
 * 
 */

public class FindSameNameMovie {
	// get info inside "";
	private static String GetInfo(String s) {
		Pattern pattern = Pattern.compile("\\S*\\\"(.+)\\\"\\S*");
		Matcher macther = pattern.matcher(s);
		if (macther.matches()) {
			String temp;
			temp = macther.group(1);
			if (temp.substring(0, 1).equals("\""))
				temp = temp.substring(1, temp.length());
			if (temp.substring(temp.length() - 1, temp.length()).equals("\""))
				temp = temp.substring(0, temp.length() - 1);
			return temp;
		} else {
			if (s.length() > 1) {
				if (s.substring(0, 1).equals("\""))
					s = s.substring(1, s.length());
				if (s.substring(s.length() - 1, s.length()).equals("\""))
					s = s.substring(0, s.length() - 1);
			}
			return s;
		}

	}

	// split one line of record string based on "," and return useful string
	// array;
	private static String[] splitt(String str) {
		String[] abc = str.split(",");
		String[] abc1 = str.split("\",\"");
		int len = abc.length;
		String[] newstr = new String[13];
		for (int i = 0; i < 12; i++) {
			if (i >= 8)
				newstr[i] = GetInfo(abc[i + 1]);
			else {
				if (i == 2 && !abc[2].equals("NULL"))
					newstr[i] = abc1[i];
				else
					newstr[i] = GetInfo(abc[i]);
			}
		}
		newstr[12] = GetInfo(abc[len - 2]);
		return newstr;
	}

	/*
	 * //if alias is used for similarity judement; //exclude the same series;
	 * private static String exclude_series(String s) { Pattern pattern =
	 * Pattern.compile(".*(第.*季).*"); Matcher macther = pattern.matcher(s); if
	 * (macther.matches()) { //System.out.println(macther.group(1)); return
	 * macther.group(1);} else return "NULL";
	 * 
	 * }
	 * 
	 * private static boolean is_series(String str1,String str2){ String
	 * a=exclude_series(str1); String b=exclude_series(str2);
	 * if(a.equals("NULL")||b.equals("NULL")) return false; else if(a.equals(b))
	 * return false; else return true;
	 * 
	 * }
	 */
	// compare whether two record is for the same series;
	private static boolean is_sameName(String[] str1, String[] str2) {
		// only use title for judgment;
		if (str1[1].equals(str2[1]) && !str1[1].equals("NULL"))
			// ||str1[2].equals(str2[2])&&!str1[2].equals("NULL")&&!is_series(str1[1],str2[1]))
			return true;
		else
			return false;
	}

	public static void main(String[] args) {
		// ***********************************************************************SETTING
		// data******
		final int RECORD_NUM = 28172;
		// all would be false when created;
		boolean[] is_compared = new boolean[RECORD_NUM];
		try {
			// *****************************************************************SETTING
			// input file******
			File file = new File("db_gxljc_dolphin--15-5-28.txt");
			File file2 = new File("IDforSameName-full.txt");
			File file3 = new File("IDforSameName.txt");
			if (!file2.exists())
				file2.createNewFile();
			if (!file3.exists())
				file3.createNewFile();
			InputStreamReader fr = new InputStreamReader(new FileInputStream(
					file), "utf-8");
			OutputStreamWriter fw = new OutputStreamWriter(
					new FileOutputStream(file2), "utf-8");
			OutputStreamWriter fw1 = new OutputStreamWriter(
					new FileOutputStream(file3), "utf-8");
			BufferedReader br = new BufferedReader(fr);
			BufferedWriter bw = new BufferedWriter(fw);
			BufferedWriter bw1 = new BufferedWriter(fw1);
			// read file info,from the second line;
			String line = br.readLine();
			String[][] allinfo = new String[RECORD_NUM][13];
			// processing info read;
			String[] info;
			// put all record into string array allinfo[][];
			for (int i = 0; i < RECORD_NUM; i++) {
				line = br.readLine();
				info = splitt(line);
				allinfo[i] = info;
			}
			for (int i = 0; i < RECORD_NUM; i++) {
				if (!is_compared[i]) {
					is_compared[i] = true;
					bw1.write(allinfo[i][0] + "###");
					bw.write(allinfo[i][0] + " ");
					bw.write(allinfo[i][1] + " ");
					System.out.print(allinfo[i][0] + " ");
					System.out.print(allinfo[i][1] + " ");

					System.out.print(allinfo[i][2] + "");
					System.out.print(allinfo[i][11] + "");
					for (int j = i + 1; j < RECORD_NUM; j++)
						if (!is_compared[j]) {
							if (is_sameName(allinfo[i], allinfo[j])) {
								is_compared[j] = true;
								bw1.write(allinfo[j][0] + "###");
								bw.write(allinfo[j][0] + " ");
								bw.write(allinfo[j][1] + " ");
								System.out.print(allinfo[j][0] + " ");
								System.out.print(allinfo[j][1] + " ");

								System.out.print(allinfo[j][2] + "");
								System.out.print(allinfo[j][11] + "");
							}
						}
					bw.write("\r\n");
					bw1.write(allinfo[i][1] + "\r\n");
					System.out.println();
					bw.flush();
					bw1.flush();
				}
			}
			fr.close();
			fw.close();
			bw.close();
			bw1.close();
		} catch (IOException e) {
			System.out.println("IO problem!");
		}
	}
}